﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

namespace SQLEditor.admin.service
{
    public class AnswerUtils
    {
        public static bool CompareDataTable(DataTable dtA, DataTable dtB)
        {
            if (dtA.Rows.Count != dtB.Rows.Count) return false;
            if (!CompareColumn(dtA.Columns, dtB.Columns)) return false;
            //比内容
            for (int i = 0; i < dtA.Rows.Count; i++)
            {
                for (int j = 0; j < dtA.Columns.Count; j++)
                {
                    if (!dtA.Rows[i][j].Equals(dtB.Rows[i][j]))
                    {
                        return false;
                    }
                }
            }
            return true;
        }


        private  static bool CompareColumn(DataColumnCollection dcA, DataColumnCollection dcB)
        {
            if (dcA.Count != dcB.Count) return false;
            foreach (DataColumn dc in dcA)
            {
                //找相同字段名称
                if (dcB.IndexOf(dc.ColumnName) < 0) return false;
                //测试数据类型
                if (dc.DataType != dcB[dcB.IndexOf(dc.ColumnName)].DataType)
                    return false;
            }
            return true;
        }

        public static string ToJson(DataTable dt)
        {
            List<Dictionary<string, object>> arrayList = new List<Dictionary<string, object>>();
            foreach (DataRow dataRow in dt.Rows)
            {
                Dictionary<string, object> dictionary = new Dictionary<string, object>();  //实例化一个参数集合
                foreach (DataColumn dataColumn in dt.Columns)
                {
                    dictionary.Add(dataColumn.ColumnName, dataRow[dataColumn.ColumnName]);
                }
                arrayList.Add(dictionary); //ArrayList集合中添加键值
            }

            return JsonUtil.ToJson(arrayList);  //返回一个json字符串
        }

        private static string[] FILTER_STR = {
                                             "exec ","grant ","revoke","truncate"
                                             };
        private static string[] FILTER_TABLE = {
                                             "sysobjects","sqlanswer ","sqlproject ","sqlquestion ","tbbug ",
                                             "tbbutton","tbdepartment","tbgoods","tbbrades","tbloginlog","tbmenu",
                                             "tbmenubutton","tbnotice","tboperatelog","tbrole","tbrolemenubutton",
                                             "tbsales","tbuser","tbuserdepartment","tbuserrole"
                                             };
        public static string filterString(string sql)
        {
            foreach (var str in FILTER_STR)
            {
                if (sql.ToLower().Contains(str)) throw new Exception("查询语句不能包含[" + str + "]关键字!");
            }
            foreach (var str in FILTER_TABLE)
            {
                if (sql.ToLower().Contains(str)) throw new Exception("查询语句不能包含[" + str + "]表名!");
            }
            return sql;
        }
    }
}